//
//  platformTaskController.m
//  pacc
//
//  Created by 测试 on 2018/5/17.
//  Copyright © 2018年 测试. All rights reserved.
//

#import "platformTaskController.h"
#import "SlideTabBarView.h"
#import "ptrwCell.h"
#import "pingtairenwuModel.h"
#import "taskDetailsCVc.h"
#import "yuepianController.h"
#import "searchController.h"
@interface platformTaskController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SlideTabBarView *slideTabBarView;
@property (strong, nonatomic)  UITableView *myTableView;
@property (copy, nonatomic) NSMutableArray * dataSource;
@property (assign, nonatomic) int pageNo;
@property(nonatomic,copy)NSString * sum;

@end

@implementation platformTaskController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self loadData];
    [self creatNavRightBtn];
}
#pragma mark --rightBtn--
-(void)creatNavRightBtn{
    UIBarButtonItem *pulishButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
    [pulishButton setTintColor:[UIColor whiteColor]];
    
    
//    UIBarButtonItem * saveButton= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"riqi_bai"] style:UIBarButtonItemStylePlain target:self action:@selector(save)];
//    [saveButton setTintColor:[UIColor whiteColor]];
    
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:pulishButton,nil]];
}
-(void)search{
    searchController * vc = [searchController new];
    vc.currentPage = 6;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)loadData{
    _pageNo = 1;
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    NSString *str =[NSString stringWithFormat:@"ordergetNotAcceptOrder"];
    NSString *nameStr =@"平台任务";
    [dic setValue:@10 forKey:@"size"];
    [dic setValue:@1 forKey:@"page"];
    [dic setValue:my_AppDelegate.token forKey:@"token"];
    
    [NetWorking startPOSTRequestWithUrl:str parameters:dic logMes:nameStr show:NO success:^(NSDictionary *data) {
        NSArray *arr = [data objectForKey:@"data"];
        self->_sum = [data objectForKey:@"sum"];
        if (self->_sum.integerValue<10) {
            self.myTableView.mj_footer.hidden = YES;
        }
        self->_dataSource = [pingtairenwuModel modelSformDics:arr];
        [self->_myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];

    } failure:^(NSError *error) {
        
    }];
}
-(void)loadMoreData{
    _pageNo ++;
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    NSString *str =[NSString stringWithFormat:@"ordergetNotAcceptOrder"];
    NSString *nameStr =@"平台任务";
    [dic setValue:zzsize forKey:@"size"];
    [dic setValue:@1 forKey:@"page"];
    [dic setValue:my_AppDelegate.token forKey:@"token"];
    
    [NetWorking startPOSTRequestWithUrl:str parameters:dic logMes:nameStr show:NO success:^(NSDictionary *data) {
        NSArray *arr = [data objectForKey:@"data"];
        if (arr&&![arr isEqual:[NSNull null]]) {
            for (NSDictionary * dic in arr) {
                pingtairenwuModel * model = [pingtairenwuModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self->_dataSource addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.myTableView reloadData];
                [self.myTableView.mj_footer endRefreshing];
            });
        }if (arr.count==0||arr.count<10){
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.myTableView reloadData];
                [self.myTableView.mj_footer endRefreshing];
                self.myTableView.mj_footer.hidden = YES;
                [self->_myTableView.mj_footer setState:MJRefreshStateNoMoreData];
            });
            
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)creatTableView{
    UITableView * myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, ZTmargin , self.view.frame.size.width, self.view.frame.size.height-64)];
    _myTableView = myTableView;
    myTableView.backgroundColor = baseBackgroundColor;
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [myTableView registerNib:[UINib nibWithNibName:@"ptrwCell" bundle:nil] forCellReuseIdentifier:@"ptrwcell"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:myTableView];
    _myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"刷新");
        [self loadData];
    }];
    _myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
}
#pragma mark --tableView delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"_dataSource.count=%lu",(unsigned long)_dataSource.count);
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ptrwCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ptrwcell" forIndexPath:indexPath];
    cell.arrData = _dataSource[indexPath.row];
    [cell.qiangdanBtn addTarget:self action:@selector(qiangdan:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.qiangdanBtn setTitle:cell.zid forState:UIControlStateHighlighted];
    [cell.yuepianBtn setTitle:cell.pacUrl forState:UIControlStateHighlighted];
    [cell.yuepianBtn addTarget:self action:@selector(yuepian:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}
-(void)yuepian:(UIButton*)sender{
    if (!my_AppDelegate.grabO) {
        return;
    }
    yuepianController *vc = [yuepianController new];
    NSString * zid = [sender titleForState:UIControlStateHighlighted];
    vc.yuepianURL = zid;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!my_AppDelegate.grabO) {
        return;
    }
    ptrwCell * cell =[tableView cellForRowAtIndexPath:indexPath];
    taskDetailsCVc *vc= [taskDetailsCVc new];
    vc.odrStatus = @"未派单";
    vc.type = 9;
    vc.zid = cell.zid;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)qiangdan:(UIButton*)sender{
    if (!my_AppDelegate.grabO) {
        return;
    }
    NSString *title = [sender titleForState:UIControlStateHighlighted];
    NSLog(@"sender.titleLabel.text=%@",title);
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否抢单"message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"抢单" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSMutableDictionary *dic =[NSMutableDictionary dictionary];
        NSString *str =[NSString stringWithFormat:@"orderacceptOrder"];
        NSString *nameStr =@"抢单";
        [dic setValue:title forKey:@"orderId"];
        [dic setValue:my_AppDelegate.token forKey:@"token"];
        
        [NetWorking startPOSTRequestWithUrl:str parameters:dic logMes:nameStr show:YES success:^(NSDictionary *data) {
            NSString *msg = [data objectForKey:@"msg"];
            NSString *status = [NSString stringWithFormat:@"%@", [data objectForKey:@"status"]];
            if ([status isEqualToString:@"200"]) {
                [SVProgressHUD showSuccessWithStatus:msg];
                [SVProgressHUD setBackgroundColor:RGB(85, 85, 85)];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self loadData];
                    [SVProgressHUD dismiss];
                });
            }
        
            [self->_myTableView reloadData];
        } failure:^(NSError *error) {
            
        }];
    }];
    
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    
    
                                          
    
    [self presentViewController:alertController animated:YES completion:nil];
}
-(void) initSlideWithCount{
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    screenBound.origin.y = 60;
    NSMutableArray * btnNameAr = [NSMutableArray arrayWithObjects:@"已接单",@"已完工",@"已审核",@"未通过",@"已付款",@"全部", nil];
    _slideTabBarView = [[SlideTabBarView alloc] initWithFrame:screenBound WithCount:btnNameAr.count withNameArr:btnNameAr];
    [self.view addSubview:_slideTabBarView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [self loadData];
    [super viewWillAppear:animated];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
